<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>工作总结类型</title>
    <script src="../../../../res/js/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="../../../../res/js/login_common.js" charset="utf-8"></script>
    <script src="../../../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../../../res/js/Tools.js" charset="utf-8"></script>
    <link rel="shortcut icon" type="image/x-icon" href="../../../../res/images/favicon.ico">
    <link rel="stylesheet" href="../../../../res/layui/css/layui.css" media="all">
</head>
<script>
    $.ajaxSetup({
        async: false
    });
</script>
<body style="padding: 10px">
<div style="display: none" id="bodys">
    <form action="" class="layui-form layui-form-pane" lay-filter="example">
        <h1 align="center" style="margin-bottom: 20px">投票类型</h1>
        <!--流程id-->
        <input name="pkVoteTypeId" id="pkVoteTypeId" type="hidden"/>
        <!--员工id-->
        <input name="personnelId" id="personnelId" type="hidden"/>

        <div class="layui-form-item">
            <label class="layui-form-label">上级类型</label>
            <div class="layui-input-block">
                <div id="voteTypeSuperId" class="xm-select-demo"></div>
            </div>
            <span id="msg">不选则为一级菜单</span>
        </div>


        <div class="layui-form-item">
            <div class="layui-form-item">
                <label class="layui-form-label">类型名称</label>
                <div class="layui-input-block">
                    <input type="text" name="voteTypeName" id="voteTypeName" lay-verify="required"
                           autocomplete="off" placeholder="请输入(必填)" class="layui-input">
                </div>
            </div>
        </div>


        <!-- 提交-->
        <div class="layui-form-item" id="sub">
            <div class="layui-input-block" id="flows">
                <button type="submit" class="layui-btn site-demo-active" lay-submit="" lay-filter="demo1">立即提交</button>
            </div>
        </div>
    </form>

</div>
<script>
    let pkVoteTypeId = getQueryString("pkVoteTypeId");
    let show = getQueryString("show");
    layui.use(['form', 'laydate'], function () {
        var  form = layui.form
            , layer = layui.layer;

        //加载组件
        layui.config({
            //引入扩展文件库
            base: getRootPath() + 'res/layui/dist/'
        }).extend({
            //引入对应的文件名
            xmSelect: 'xm-select'
            //拿到对应的文件模块
        }).use(['xmSelect'], function () {
            //对于模块赋值使用
            var xmSelect = layui.xmSelect;
            //渲染多选
            xmSelect.render({
                el: '#voteTypeSuperId',
                radio: true,
                name:'voteTypeSuperId',
                tree: {
                    show: true,
                    strict: false,
                    expandedKeys: [-1],
                },
                height: 'auto',
                data: loadVoteType(pkVoteTypeId, show)
            })
        });

        function loadVoteType(id, show) {
            let url = getRootPath() + "OaVoteTypeAction/loadVoteType";
            let data = {
                id: id,
                show: show
            };
            let res = getAjax(url, data);
            if (res) {
                return res;
            } else {
                return [];
            }

        }


        let res;
        if (pkVoteTypeId) {
            res = getAjaxById(getRootPath() + "OaVoteTypeAction/findById/" + pkVoteTypeId);
            //工作总结id
            $("#pkVoteTypeId").val(res.pkVoteTypeId);
            //本阶段总结
            let voteTypeName = $("#voteTypeName");
            voteTypeName.val(res.voteTypeName);

            //判断是否是查看
            if (show) {
                //投票名称
                formDisabled(voteTypeName);
                //上级类型
                formDisabled($("#voteTypeSuperId"));
                //隐藏提交按钮
                $("#sub").hide();

            }
            form.render();
        }

        form.render();

        //监听起草提交
        form.on('submit(demo1)', function (data) {

            // 单击之后提交按钮不可选,防止重复提交
            var DISABLED = 'layui-btn-disabled';
            // 增加样式
            $('.site-demo-active').addClass(DISABLED);
            // 增加属性
            $('.site-demo-active').attr('disabled', 'disabled');
            const res = postAjax(getRootPath() + "OaVoteTypeAction/add", data.field);
            if (res.code === 1) {
                layer.msg('新增成功3秒后关闭当前窗口', {icon: 1});
                setTimeout(function () {
                    formClose();
                }, 3000);
                setTimeout(function () {
                    parent.layui.table.reload('test');//刷新父页面
                    //window.parent.location.reload();//刷新父页面
                    const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                }, 3000);
                //window.location.reload();//刷新页面
            } else if (res.code === 500) {
                error('500');
            } else {
                layer.msg("新增失败");
            }
            return false;
        });

        /**
         * 禁用
         *  @param forms 控件
         */
        function formDisabled(forms) {
            const DISABLED = 'layui-btn-disabled';
            forms.attr('disabled', 'disabled');
            forms.addClass(DISABLED);
        }


        $("#bodys").show();
        form.render();
    });
</script>
</body>
</html>